package com.amigo.online.message.mongo.dao.impl;

import com.amigo.online.message.mongo.dao.ConstantValue;
import com.amigo.online.message.mongo.dao.GroupDao;
import com.amigo.online.message.mongo.entity.GroupEntity;
import com.amigo.online.message.mybatisbean.GroupTbl;
import com.mongodb.BasicDBObject;
import com.mongodb.client.AggregateIterable;
import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class GroupDaoImpl implements GroupDao {

    @Autowired
    @Qualifier("readMongoTemplate")
    MongoTemplate readMongoTemplate;

    @Autowired
    @Qualifier("writeMongoTemplate")
    MongoTemplate writeMongoTemplate;

    @Autowired
    private GroupDao groupDao;

    private static final int SIZE = 10;
    /**
     * 最大距离 3000公里
     */
    private static final int MAX_DISTANCE = 3000000;

    @Override
    public Object nearbyGroup(Integer page) {
        BasicDBObject obQuery = new BasicDBObject();
        obQuery.put("status", 1);
        obQuery.put("disbanded", 0);
        List<BasicDBObject> pipeline = new ArrayList<>();
        if (null == page || page <= 0) {
            page = 1;
        }
        BasicDBObject skipObject = new BasicDBObject("$skip", (page - 1) * SIZE);
        pipeline.add(skipObject);
        BasicDBObject limitObject = new BasicDBObject("$limit", SIZE);
        pipeline.add(limitObject);
        Double[] point = new Double[]{};
        BasicDBObject aggregate = new BasicDBObject("$geoNear",
                new BasicDBObject("near", new BasicDBObject("type", "Point").append("coordinates", point))
                        .append("distanceField", "distance").append("query", obQuery)
                        .append("maxDistance", MAX_DISTANCE)
                        .append("spherical", true));
        pipeline.add(0, aggregate);
        AggregateIterable<Document> iterators = readMongoTemplate.getCollection(ConstantValue.COLLECTION_USER).aggregate(pipeline);
        return iterators;
    }

    @Override
    public boolean insertGroup(GroupTbl group) {

        System.out.println(group);

        GroupEntity groupEntity = new GroupEntity();
        BeanUtils.copyProperties(group, groupEntity);

        groupEntity.setCountry(0);
        groupEntity.setProvince(0);
        groupEntity.setCity(0);
        groupEntity.setDisbanded_time(0L);

        groupEntity.setUser_id(group.getUserId());
        groupEntity.setIntroduction(group.getIntroduction());
        if(group.getInvisible()!=null){
            groupEntity.setInvisible(group.getInvisible() ? 1 : 0);
        }
        GeoJsonPoint loc = new GeoJsonPoint(Double.valueOf(group.getLongitude()), Double.valueOf(group.getLatitude()));
        groupEntity.setLoc(loc);
        groupEntity.setAddress(group.getAddress());
        groupEntity.setAddressName(group.getAddress());
        groupEntity.setAvatar(group.getAvatar());
        groupEntity.setChecked_time(group.getCheckedTime());
        groupEntity.setCreated_user_id(group.getCreatedUserId());
        if(group.getDisbanded()!=null){
            groupEntity.setDisbanded(group.getDisbanded() ? 1 : 0);
        }
        groupEntity.setId(group.getId());
        groupEntity.setName(group.getName());
        if(group.getStatus()!=null){
            groupEntity.setStatus(group.getStatus() ? 1 : 0);
        }
        groupEntity.setUpdated_time(group.getUpdatedTime());

        writeMongoTemplate.save(groupEntity, "groupEntity");
        return true;
    }
    public static void main(String[] args) {
        System.out.println( true  ? 1 : 0);
    }
}
